﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using iPhotoUploader.Library;

namespace iPhotoUploader.GUI
{
    public partial class SelectEventForm : Form
    {
        public SelectEventForm()
        {
            InitializeComponent();
        }

        private void SelectCategoryForm_Load(object sender, EventArgs e)
        {
            try
            {
                Settings.EventId = string.Empty;
                activeexposure.iPhoto active = new activeexposure.iPhoto();
                int[] years = active.GetAllYears();

                Dictionary<string, string> values = new Dictionary<string, string>();
                values.Add("0", "- Select -");
                foreach (int year in years.OrderByDescending(y => y))
                {
                    values.Add(year.ToString(), year.ToString());
                }

                ddlYears.DisplayMember = "Value";
                ddlYears.ValueMember = "Key";
                ddlYears.DataSource = new BindingSource(values, null);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void ddlYears_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (ddlYears.SelectedValue.ToString() != "0")
                {
                    int year = Int32.Parse(ddlYears.SelectedValue.ToString());
                    activeexposure.iPhoto active = new activeexposure.iPhoto();
                    activeexposure.EventLight[] evts = active.GetEventsByYear(year);

                    Dictionary<string, string> values = new Dictionary<string, string>();
                    values.Add("0", "- Select -");
                    foreach (activeexposure.EventLight evt in evts)
                    {
                        values.Add(evt.Id.ToString(), evt.Name);
                        foreach (activeexposure.EventLight subEvt in evt.SubEvents)
                        {
                            values.Add(subEvt.Id.ToString(), "  " + subEvt.Name);
                        }
                    }

                    ddlEvents.DisplayMember = "Value";
                    ddlEvents.ValueMember = "Key";
                    ddlEvents.DataSource = new BindingSource(values, null);
                    ddlEvents.Enabled = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (ddlEvents.SelectedValue.ToString() != "0")
            {
                Settings.EventId = ddlEvents.SelectedValue.ToString();
                Settings.EventName = ddlEvents.Text;

                foreach (Form openForm in Application.OpenForms)
                {
                    if (openForm.Name == "MainForm")
                        ((MainForm)openForm).SetLabel(Settings.EventName);
                }

            }
            this.Close();
        }
    }
}
